package cdc.cs.service;

import cdc.cs.common.HttpWithAuthService;
import cdc.cs.config.ODataConfig;
import cdc.cs.entity.BusinessPartnersBankVO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.IOException;

/**
 * @ClassName: ODataPartnersBankService
 * @Description: TODO
 * @Author: kevin
 * @Date: 2020/5/28 15:28
 * @Version: 1.0
 */
@Service
@Slf4j
public class ODataPartnersBankService {
    private BusinessPartnersBankVO bankVO;

    /**
     * 获取单个银行
     * @param url
     * @return
     */
    public String getOneBank(String url) {
        String result = null;
        int count = 2;
        while (count > 0) {
            try {
                result = HttpWithAuthService.getService(url);
            } catch (IOException e) {
                log.error("获取Partner总数网络异常，异常信息为:{}，地址为:{}", e.getMessage(), url);
            }
            if (StringUtils.isEmpty(result)) {
                count--;
            } else {
                break;
            }

        }
        return result;
    }

    /**
     * 解析银行
     * @param response
     * @return
     */
    private BusinessPartnersBankVO parseBankJson(String response) {
        BusinessPartnersBankVO bankVO = new BusinessPartnersBankVO();
        JSONObject obj = (JSONObject) JSONObject.parse(response);
        JSONObject d = (JSONObject) obj.get("d");
        JSONArray results = d.getJSONArray("results");
        if (results.size() > 0) {
            for (int i = 0; i < results.size(); i++) {
                JSONObject object = results.getJSONObject(i);
                bankVO.setBusinessPartner(object.getString("BusinessPartner"));
                bankVO.setBankAccount(object.getString("BankAccount"));
                bankVO.setBankCountryKey(object.getString("BankCountryKey"));
                bankVO.setBankAccountName(object.getString("BankAccountName"));
                bankVO.setBankNumber(object.getString("BankNumber"));
                bankVO.setValidityStartDate(object.getString("ValidityStartDate"));
                bankVO.setValidityEndDate(object.getString("ValidityEndDate"));

            }
            this.bankVO = bankVO;
            return bankVO;
        } else {
            return null;
        }

    }

    /**
     * 数据转换
     * @param bankVO
     * @return
     */
    public JSONObject dataChange(BusinessPartnersBankVO bankVO) {
        JSONObject result = new JSONObject();
        result.put("BusinessPartner", bankVO.getBusinessPartner());
        result.put("BankAccount", bankVO.getBankAccount());
        result.put("BankAccountName", bankVO.getBankAccountName());
        result.put("BankCountryKey", bankVO.getBankCountryKey());
        result.put("BankNumber", bankVO.getBankNumber());
        result.put("ValidityEndDate", bankVO.getValidityEndDate());
        result.put("ValidityStartDate", bankVO.getValidityStartDate());
        return result;
    }

    public boolean savePartnerBank(BusinessPartnersBankVO bankVO) {
        JSONObject jsonObject = dataChange(bankVO);
        int count = 2;
        boolean flag = false;
        while (count > 0) {
            try {
                HttpWithAuthService.postService(ODataConfig.targetPartnerBank, jsonObject);
                flag = true;
            } catch (IOException e) {
                log.error("保存PartnerBank出现异常，异常信息为:{}，地址为:{}", e.getMessage(), ODataConfig.targetPartnerBank);
            }
            if (flag) {
                break;
            } else {
                count--;
            }
        }
        return flag;
    }

    public boolean savePartnerBank(String url) {
        BusinessPartnersBankVO bankVO = queryOneBank(url);
        if (bankVO == null) {
            return false;
        } else {
            boolean result = savePartnerBank(bankVO);
            if (!result) {
                return false;
            }
            return true;
        }
    }

    /**
     * 删除银行
     * @param url
     */
    public void deletePartnerBank(String url) {
        int count = 2;
        boolean flag = false;
        while (count > 0) {
            try {
                HttpWithAuthService.deleteService(url);
                flag = true;
            } catch (IOException e) {
                log.error("删除TargetPartnerBank异常，异常信息为:{}，地址为:{}", e.getMessage(), url);
            }
            if (flag) {
                break;
            } else {
                count--;
            }
        }
    }

    /**
     * 获取数据
     * @param url
     * @return
     */
    public BusinessPartnersBankVO queryOneBank(String url) {
        String oneBank = getOneBank(url);
        if (StringUtils.isEmpty(oneBank)) {
            return null;
        } else {
            return parseBankJson(oneBank);
        }
    }
}
